/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/03/15
 */
package vn.smartware.co.server.dao.registration;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import vn.smartware.co.dto.MstFoodDTO;
import vn.smartware.co.dto.MstPriceDTO;
import vn.smartware.co.dto.MstUnitDTO;
import vn.smartware.co.forms.RegisterFoodForm;
import vn.smartware.co.framework.bean.LoginDTO;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.java.net.commons.Utils;
import vn.smartware.co.server.dao.factory.AbstractRegisterFoodDAO;
import vn.smartware.co.server.sql.factory.AbstractRegisterFoodSQLManager;
import vn.smartware.co.server.sql.factory.SQLFactory;

/**
 * @author thuctt
 * RegisterFoodDAO
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class RegisterFoodDAO extends AbstractRegisterFoodDAO {

	private static Log log = LogFactory.getLog(RegisterFoodDAO.class);
	
	/**
	 * getUnitList
	 * @param form 
	 * @param user 
	 * @param conn
	 * @return ArrayList
	 * @throws ServerException
	 */
	public ArrayList getUnitList(RegisterFoodForm form, LoginDTO user,
			Connection conn) throws ServerException {
		
		AbstractRegisterFoodSQLManager manager = SQLFactory.createRegisterFoodSQLManager();
		ResultSet rs = null;
		int count = 0;
		ArrayList list = new ArrayList();
		try {
			manager.makeGetUnitListSQL(user, conn);
			rs = manager.executeQuery();
			
			while (rs.next()){
				MstUnitDTO unit = new MstUnitDTO();
				unit.setUnitCd(rs.getString("UNIT_CD"));
				unit.setName(rs.getString("NAME"));
				unit.setDescription(rs.getString("DESCRIPTION"));
				
				list.add(unit);
				count++;
			}
			
			log.info(count + " records.");
			return list;
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
		} finally {
			try {
				manager.close(rs);
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
	}
	
	/**
	 * get food list
	 * @param form 
	 * @param user 
	 * @param conn
	 * @return ArrayList
	 * @throws ServerException
	 */
	public ArrayList getFoodList(RegisterFoodForm form, LoginDTO user,
			Connection conn) throws ServerException {
		
		AbstractRegisterFoodSQLManager manager = SQLFactory.createRegisterFoodSQLManager();
		ResultSet rs = null;
		int count = 0;
		ArrayList foodList = new ArrayList();
		ArrayList priceList = new ArrayList();
		MstFoodDTO food = new MstFoodDTO();
		try {
			manager.makeGetFoodListSQL(form, user, conn);
			rs = manager.executeQuery();
			
			String preFoodNo = "";
			while (rs.next()){
				String foodNo = rs.getString("FOOD_NO");
				if (!Utils.isNullEmpty(preFoodNo) && !foodNo.equals(preFoodNo)){
					food.setPriceList(priceList);
					foodList.add(food);
					food = new MstFoodDTO();
					priceList = new ArrayList();
				}
				
				food.setFoodNo(rs.getString("FOOD_NO"));
				food.setName(rs.getString("NAME"));
				food.setDescription(rs.getString("DESCRIPTION"));
				food.setUnitCd(rs.getString("UNIT_CD"));
				food.setUnitName(rs.getString("UNIT_NAME"));
				
				String priceNo = Utils.toEmpty(rs.getString("PRICE_NO"));
				if (!Utils.isNullEmpty(priceNo)){
					MstPriceDTO price = new MstPriceDTO();
					price.setPriceNo(rs.getString("PRICE_NO"));
					price.setPrice(rs.getString("PRICE"));
					price.setMoneyType(rs.getString("MONEY_TYPE"));
					price.setMoneyTypeName(rs.getString("MONEY_TYPE_NAME"));
					priceList.add(price);
				}
				
				preFoodNo = foodNo;
				count++;
			}
			
			log.info(count + " records.");

			if (count > 0){
				food.setPriceList(priceList);
				foodList.add(food);
			}
			
			return foodList;
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
		} finally {
			try {
				manager.close(rs);
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
	}

	/**
	 * deletePriceList
	 * @param foodNo
	 * @param user
	 * @param conn
	 * @throws ServerException
	 */
	public void deletePriceList(String foodNo, LoginDTO user, Connection conn)
			throws ServerException {

		AbstractRegisterFoodSQLManager manager = SQLFactory.createRegisterFoodSQLManager();
		int count = 0;
		try {
			manager.makeDeletePriceListSQL(foodNo, user, conn);
			count = manager.executeUpdate();
			log.info(count + " rows deleted.");
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
			
		} finally {
			try {
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
	}

	/**
	 * deleteRoom
	 * @param foodNo
	 * @param user
	 * @param conn
	 * @throws ServerException
	 */
	public void deleteFood(String foodNo, LoginDTO user, Connection conn)
			throws ServerException {

		AbstractRegisterFoodSQLManager manager = SQLFactory.createRegisterFoodSQLManager();
		int count = 0;
		try {
			manager.makeDeleteFoodSQL(foodNo, user, conn);
			count = manager.executeUpdate();
			log.info(count + " rows deleted.");
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
			
		} finally {
			try {
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
	}

	/**
	 * registerFood
	 * @param room
	 * @param user
	 * @param conn
	 * @return String
	 * @throws ServerException
	 */
	public String registerFood(MstFoodDTO room, LoginDTO user, Connection conn)
			throws ServerException {

		AbstractRegisterFoodSQLManager manager = SQLFactory.createRegisterFoodSQLManager();
		ResultSet rs = null;
		int count = 0;
		String foodNo = "";
		try {
			manager.makeRegisterFoodSQL(room, user, conn);
			count = manager.executeUpdate();
			log.info(count + " rows updated.");
			//get room no just registered
			manager = SQLFactory.createRegisterFoodSQLManager();
			manager.getMaxFoodNoSQL(conn);
			rs = manager.executeQuery();
			if (rs.next()){
				foodNo = rs.getString("FOOD_NO");
			}
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
			
		} finally {
			try {
				manager.close(rs);
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
		return foodNo;
	}

	/**
	 * updateFood
	 * @param room
	 * @param user
	 * @param conn
	 * @throws ServerException
	 */
	public void updateFood(MstFoodDTO room, LoginDTO user, Connection conn)
			throws ServerException {

		AbstractRegisterFoodSQLManager manager = SQLFactory.createRegisterFoodSQLManager();
		int count = 0;
		try {
			manager.makeUpdateFoodSQL(room, user, conn);
			count = manager.executeUpdate();
			log.info(count + " rows updated.");
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
			
		} finally {
			try {
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
	}

	/**
	 * registerPrice
	 * @param foodNo
	 * @param price
	 * @param user
	 * @param conn
	 * @throws ServerException
	 */
	public void registerPrice(String foodNo, MstPriceDTO price, LoginDTO user,
			Connection conn) throws ServerException {
	
		AbstractRegisterFoodSQLManager manager = SQLFactory.createRegisterFoodSQLManager();
		int count = 0;
		try {
			manager.makeRegisterPriceSQL(foodNo, price, user, conn);
			count = manager.executeUpdate();
			log.info(count + " rows updated.");
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
			
		} finally {
			try {
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
	}

	
}
